package offer;

/**
 * @Author Elephas
 * @Date 2022/1/28
 **/
@FunctionalInterface
public interface MajorityElement {
    int majorityElement(int[] nums);
}

class MajorityElementImpl1 implements MajorityElement{

    @Override
    public int majorityElement(int[] nums) {
        int currentElement = nums[0];
        int frequency = 1;
        for (int i = 1; i < nums.length; i++) {
            if(nums[i] == currentElement){
                frequency++;
            }else{
                if(frequency == 1){
                    currentElement = nums[i];
                }else{
                    frequency--;
                }
            }
        }
        return currentElement;
    }
}
